package itheima.com.springbootwebquickstart01.Server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(8080);//监听制定端口
        System.out.println("server is running.....");

        while (true){
            Socket accept = serverSocket.accept();
            System.out.println("client is coming.....");
            System.out.println("connected from"+accept.getRemoteSocketAddress());
            Thread thread = new Handler(accept);
            thread.start();
        }

//        try {
//            ServerSocket ss = new ServerSocket(8888);
//            System.out.println("启动服务器....");
//            Socket s = ss.accept();
//            System.out.println("客户端:"+s.getInetAddress().getLocalHost()+"已连接到服务器");
//
//            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//            //读取客户端发送来的消息
//            String mess = br.readLine();
//            System.out.println("客户端："+mess);
//            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//            bw.write(mess+"\n");
//            bw.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }



    }

}


class Handler extends Thread{
    private Socket accept;

    public Handler(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
//        super.run();
        try {
            InputStream inputStream = this.accept.getInputStream();
            OutputStream outputStream = this.accept.getOutputStream();
            handle(inputStream,outputStream);
        } catch (IOException e) {
            try {
                this.accept.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
    }

    public void handle(InputStream inputStream,OutputStream outputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));

        boolean requestOk = false;
        String first = reader.readLine();
        if(first.startsWith("GET / HTTP/1.")){
            requestOk = true;
        }

        for (;;){
            String header = reader.readLine();
            if (header.isEmpty()){
                break;
            }
            System.out.println(header);
        }
        System.out.println(requestOk?"Response OK":"Response Error");
        if (!requestOk){
            writer.write("HTTP/1.1 404 NOT FOUND\r\n");
            writer.write("Content-Length:  o\r\n");
            writer.write("\r\n");
            writer.flush();
        }else {
            InputStream is = Server.class.getClassLoader().getResourceAsStream("html/a.html");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            StringBuilder data = new StringBuilder();
            String line = null;
            while ((line = bufferedReader.readLine()) != null){
                data.append(line);
            }
            bufferedReader.close();
            int length = data.toString().getBytes(StandardCharsets.UTF_8).length;

            writer.write("HTTP/1.1 200 OK\r\n");
            writer.write("Connection: keep-alive\r\n");
            writer.write("Content-Length: "+length+"\r\n");
            writer.write("\r\n");
            writer.write(data.toString());
            writer.flush();

        }

    }




}


